require 'config/environment'

task :estimate_level do
  raise "IO Error: Please specify csv file(UTF-8)" unless ENV.has_key? "doc"
  
  threshold = ENV.has_key?("threshold") ? ENV["threshold"].to_i : 1000
  rs = ENV.has_key?("rs") ? ENV["rs"] : "\t"
  require 'csv'
  new_words = 0
  
  CSV.open(ENV["doc"],"r",rs) do |row|
    next if row[0..2].empty?
    raise "Format Error: Different column size. Column size should be 2 columns." if row[0..2].size != 3
    raise "Format Error: First column should not be empty." if row[0].blank?
    raise "Format Error: Third column should not be integer(>-1)." if /^\d+$/ !~ row[2]
    
    frequency = (row[2].to_i > threshold) ? threshold : row[2].to_i
    level = Math.log(frequency) / Math.log(threshold)
    
    dic = Dictionary.find(:all,:conditions => {:index => row[0]})
    candidates = dic.select{|one| one.entry["reading"] == row[1] }
    candidates.each do |entry|
      entry.update_attribute :estimate_level,level
    end
    next unless candidates.empty?
    
    unless dic.empty?
      dic.each do |entry|
        next if entry.estimate_level != nil && entry.estimate_level > level
        entry.update_attribute :estimate_level,level
      end
    else
      Dictionary.create :index => row[0],:entry => {"surface" => row[0].to_s},:estimate_level => level
      new_words += 1
    end
    
  end
  
  puts "create new #{new_words} entries" if new_words > 0
end